package com.example.front.serivce;

import com.example.common.dto.ListDto;
import com.example.common.dto.Pagination;
import com.example.common.exception.BusinessException;
import com.example.front.dto.DashboardSearchDto;
import com.example.front.entity.Dashboard;
import com.example.front.entity.Level;
import com.example.front.mapper.DashboardMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class DashboardService {

    @Resource
    DashboardMapper dashboardMapper;
    @Resource
    BoxDashboardService boxDashboardService;

    /**
     * 新增看板
     */
    @Transactional
    public void createDashboard(long id, String name) {
        Dashboard dashboard = Dashboard.builder().
                tenantId(id).name(name).build();
        if (name.trim().equals("")) {
            throw new BusinessException("看板名称不能为空");
        }
        List<Level> list = new ArrayList<>();

        if (!Objects.equals(dashboardMapper.find(dashboard, new Pagination()), list)) {
            throw new BusinessException("看板名称已存在");
        }
        dashboardMapper.create(id, name);
    }

    /**
     * 获取看板列表
     */
    public ListDto<Dashboard> listDashboard(DashboardSearchDto condition, Pagination pagination) {
        ListDto<Dashboard> listDto = new ListDto<>();

        Dashboard dashboard = new Dashboard();
        BeanUtils.copyProperties(condition, dashboard);

        long total = dashboardMapper.count(dashboard);
        if (total <= 0) {
            return new ListDto<>(new ArrayList<>(), pagination);
        }
        pagination.setTotal(total);

        listDto.setList(dashboardMapper.find(dashboard, pagination));
        for (Dashboard dashboards : listDto.getList()) {
            dashboards.setBoxList(boxDashboardService.list(dashboards));
        }


        listDto.setPagination(pagination);

        return listDto;
    }

    /**
     * 修改看板(名称)信息
     */
    @Transactional
    public void updateDashboard(Dashboard dashboard) {
        List<Level> list = new ArrayList<>();
        if (!Objects.equals(dashboardMapper.find(dashboard, new Pagination()), list)) {
            throw new BusinessException("名称已存在");
        }
        if (dashboardMapper.update(dashboard) != 1) {
            throw new BusinessException("修改失败");
        }
    }

    /**
     * 删除看板
     */
    @Transactional
    public void deleteDashboard(long tenantId, long id) {
        if (dashboardMapper.delete(tenantId, id) != 1) {
            throw new BusinessException("删除失败");
        }
        boxDashboardService.deleteById(id, tenantId);

    }

    /**
     * 获取看板信息
     */
    public Map<Long, Dashboard> findByIds(List<Long> dashboardIds) {

        Map<Long, Dashboard> categoryMap = dashboardMapper.findDashboardByIds(dashboardIds).stream()
                .collect(Collectors.toMap(Dashboard::getId, dashboard -> dashboard));

        return categoryMap;
    }

}
